#include <QAbstractItemView>

#include "ctableview.h"

CTableView::CTableView(QWidget *parent): QTableView(parent)
{
    setContextMenuPolicy(Qt::CustomContextMenu);
    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setDragEnabled(true);
    setDragDropMode(QAbstractItemView::DragOnly);
    setSelectionMode(QAbstractItemView::SingleSelection);
    setSelectionBehavior(QAbstractItemView::SelectRows);
}

void CTableView::resizeColumns()
{
    if(!m_count)
        return;

    int width = viewport()->width()/m_count;

    for(int i=0; i<m_count-1; i++)
        setColumnWidth(i, width);
    setColumnWidth(m_count-1, viewport()->width()-width*(m_count-1));

    resizeRowsToContents();
}

void CTableView::columnCountChanged(int, int count)
{
    m_count = count;
}

void CTableView::resizeEvent(QResizeEvent *e)
{
    QTableView::resizeEvent(e);
    resizeColumns();
}

void CTableView::setColsCount(int count)
{
    m_count = count;
}
